package cn.wolfcode.util;

import java.util.Calendar;
import java.util.Date;


public class DateUtil {
    /**
     * 根据日期和场次看是否在秒杀有效时间之内
     * @param date 秒杀商品的开始时间
     * @param time 场次
     * @return
     */
    public static boolean isLegalTime(Date date, int time) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);    //2020-12-31 00.00.00
        c.set(Calendar.HOUR_OF_DAY, time);  //2020-12-31 10.00.00
        Long start = c.getTime().getTime(); //c.getTime() 获取Data对象 c.getTime().getTime()获取距离1970毫秒值
        Long now = System.currentTimeMillis();  //获取的当前时间的时间戳
        c.add(Calendar.HOUR_OF_DAY, 2); //2020-12-31 12.00.00
        Long end = c.getTime().getTime();   //获取结束时间的时间戳
        return now >= start && now <= end;
    }
}
